package controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import beans.EventBean;
import beans.MessageBean;
import beans.UserBean;

import model.EventDAO;
import model.MessageDAO;
import model.UserDAO;

public class LoginServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException {
		try
		{	    
			HttpSession session = request.getSession(true);
			UserBean user = new UserBean();
			ArrayList<MessageBean> recentmessages = new ArrayList<MessageBean>();
			if (session.getAttribute("currentSessionUser") != null) {
				UserBean currentUser = (UserBean)session.getAttribute("currentSessionUser");
				if (currentUser.getEmail().equals(request.getParameter("un")) || (request.getParameter("un")) == null)
					user = currentUser;
				else {
					user.setEmail(request.getParameter("un"));
					user.setPassword(request.getParameter("pw"));
				}
			}
			else {
				user.setEmail(request.getParameter("un"));
				user.setPassword(request.getParameter("pw"));
			}
			user = UserDAO.login(user);
			if (user.isValid())
			{	        
				recentmessages = MessageDAO.recentMessages(user, true);
				ArrayList<EventBean> usersevent = EventDAO.getUsersEvents(user);
				for (EventBean event : usersevent) {
					event.setNotification(false);
					if (event.isFinished()) {
						event.setGuestList(EventDAO.getGuests(event));
						if (!event.getGuestList().isEmpty()) {
							event = EventDAO.getEventsRatings(user, event);
							if (event.getHost().equals(user.getEmail()))
								for (UserBean guest : event.getGuestList()) {
									if ((guest.getEmail() != null) && (guest.getHostToGuestRating() == 0)) {
										event.setNotification(true);
									}
								}
							else
								for (UserBean guest : event.getGuestList()) {
									if ((guest.getEmail().equals(user.getEmail())))
										if ((event.getHost() != null) && (guest.getGuestToHostRating() == 0))
											event.setNotification(true);
								}
						}
					}
				}
				int hostedCount = 0;
				int attendedCount = 0;
				int hostedRatedCount = 0;
				int attendedRatedCount = 0;
				double hostedSum = 0;
				double attendedSum = 0;
				double thisEventGuestSum = 0;
				int thisEventGuestCount = 0;
				for (EventBean event : usersevent) {
					if (event.isFinished()) {
						event.setGuestList(EventDAO.getGuests(event));
						EventBean eventduplo = EventDAO.getEventsRatingsForStats(user, event);
						if (eventduplo.getHost().equals(user.getEmail())) {
							hostedCount++;
							for (UserBean guest : eventduplo.getGuestList()) {
								if (guest.getGuestToHostRating() != 0) {
									thisEventGuestSum += guest.getGuestToHostRating();
									thisEventGuestCount++;
								}
							}
							if (thisEventGuestCount != 0) {
								hostedRatedCount++;
								hostedSum += (thisEventGuestSum/thisEventGuestCount);
							}
						} else {
							attendedCount++;
							for (UserBean guest : eventduplo.getGuestList()) {
								if (guest.getEmail().equals(user.getEmail())) 
									if (guest.getHostToGuestRating() != 0) {
										attendedRatedCount++;
										attendedSum += guest.getHostToGuestRating();
									}
							}
						}
					}
				}
				user.setTotalAttended(attendedCount);
				user.setTotalHosted(hostedCount);
				if (hostedRatedCount != 0)
					user.setHostRatingAvg(hostedSum/hostedRatedCount);
				else
					user.setHostRatingAvg(0.00);
				if (attendedRatedCount != 0)
					user.setGuestRatingAvg(attendedSum/attendedRatedCount);
				else
					user.setGuestRatingAvg(0.00);
				session.setAttribute("failedLogin", false);
				session.setAttribute("usersevent", usersevent);
				session.setAttribute("currentSessionUser",user); 
				session.setAttribute("recentmessages", recentmessages);
				response.sendRedirect("home.jsp"); //logged-in page      		
			}
			else {
				session.setAttribute("failedLogin", true);
				response.sendRedirect("login.jsp"); //loopback to login page to display error message
			}
		} 
		catch (Throwable theException) 	    
		{
			System.out.println(theException); 
		}
	}
}
